<?php
require_once("rpcl/rpcl.inc.php");
require_once("config/data.php");
//Includes
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");
use_unit("comctrls.inc.php");
use_unit("buttons.inc.php");



//Class definition
class Teahcer extends Page
{
    public $Buttonsxxmqxsh = null;
   public $Panel_SxxmglMenu = null;
   public $ComboBoxStuClass = null;
   public $ButtonCanelAdd = null;
   public $Button_fb = null;
   public $Button_qxfb = null;
   public $sxtitle = null;
   public $LabelTeacherName = null;
   public $LabelDeparment = null;
   public $PanelAddExp = null;
   public $LabelCourseName = null;
   public $LabelClassName = null;
   public $ButtonQrAdd = null;
   public $LabelKeShi = null;
   public $EditKeShi = null;
   public $LabelExpName = null;
   public $EditExpName = null;
   public $ComboBoxStuProf = null;
   public $LabelStuProfName = null;
   public $ComboBoxCourseName = null;
   public $ButtonAdd = null;
   public $ButtonDelete = null;
   public $ButtonAddNext = null;
   public $ButtonLoginOut = null;
   public $Buttonsxxmsh = null;
   public $Panelsxxmsh = null;




/*******************************************************************************/
                      /*以下部分为实训项目管理部分代码 */
/*******************************************************************************/

  /*------------------------页面初始化------------------------
        1.查找已经添加的项目并在项目管理中显示
        2.加载实训报告列表
        3.根据不同教师权限加载不同的选项卡
-----------------------------------------------------------------------  */
   function TeahcerCreate($sender, $params)
   {
     global $dzsxDB;
     $this->TemplateEngine='SmartyTemplate';
     $this->TemplateFilename="template/teacher.html";
     if(!isset($_SESSION['TeacherId']))
       redirect("index.php");
     else
     {

      $v = $_SESSION['TeacherId'];

       //加载教师信息列表
       $dzsxDB->QueryTeacherinfo->Active=False;
       $dzsxDB->QueryTeacherinfo->SQL="Select teacher.TeacherId ,  teacher.TeacherName,
       teacher.TeaProf From teacher where teacher.TeacherId = '$v'";
       $dzsxDB->QueryTeacherinfo->Active=True;
       if($dzsxDB->QueryTeacherinfo->RecordCount!==0)
       {
        $this->LabelTeacherName->caption = "教师姓名：" . $dzsxDB->QueryTeacherinfo->TeacherName;
        $this->LabelDeparment->caption = "所在部门：" . $dzsxDB->QueryTeacherinfo->TeaProf;
       }
       else
        echo "No user!";

     if(isset($_GET[delid]))
       {
        $delid=$_GET[delid];
        $dzsxDB->QueryDelExinfo->Active=False;
        $dzsxDB->QueryDelExinfo->SQL = "delete from expinfo where ExpId=$delid";
        $dzsxDB->QueryDelExinfo->Active = True;
       }
     }
   }

   /**
    根据不同教师权限加载smarty模板中不同的选项卡
   **/
   function TeahcerTemplate($sender, $params)
    {
       $template=$params['template'];
       $TeaType=$_SESSION['TeacherType'];
       $template->_smarty->assign('TeaType',$TeaType);
    }

    /*------在课程添加选择列表框中添加课程--------*/
   function ComboBoxCourseNameBeforeShow($sender, $params)
   {
      global $dzsxDB;
      $this->ComboBoxCourseName->clear();//防止重复添加选项，每次刷新时先清空
      $dzsxDB->QueryTeacherCourse->Active = false;
      $dzsxDB->QueryTeacherCourse->SQL = "Select * from course Order By CourseName";
      $dzsxDB->QueryTeacherCourse->Active = true;
      $dzsxDB->QueryTeacherCourse->first();
      while( ! $dzsxDB->QueryTeacherCourse->EOF)
      {
         $this->ComboBoxCourseName->AddItem($dzsxDB->QueryTeacherCourse->CourseName, null, $dzsxDB->QueryTeacherCourse->CourseId);
         $dzsxDB->QueryTeacherCourse->next();
      }
   }

   /*------在专业添加选择列表框中添加专业--------*/
   function ComboBoxStuProfBeforeShow($sender, $params)
   {
      global $dzsxDB;
      $this->ComboBoxStuProf->clear();
      $dzsxDB->QueryStuProf->Active = False;
      $dzsxDB->QueryStuProf->SQL ="Select * From prof Order By ProfId";
      $dzsxDB->QueryStuProf->Active = True;
      $dzsxDB->QueryStuProf->first();
      $this->ComboBoxStuProf->ItemIndex=$dzsxDB->QueryStuProf->ProfId;
      while( ! $dzsxDB->QueryStuProf->EOF)
      {
         $this->ComboBoxStuProf->AddItem($dzsxDB->QueryStuProf->ProfName, null, $dzsxDB->QueryStuProf->ProfId);
         $dzsxDB->QueryStuProf->next();
      }
      $this->SelcetClassByProf($sender,$this->ComboBoxStuProf->ItemIndex);
   }

   /*------根据专业选择班级--------*/
   function ComboBoxStuProfJSChange($sender, $params)
    {
        ?>
        //begin js
        params=$("#ComboBoxStuProf").val();
        $("#ButtonQrAdd").attr("disabled", true);
        $("#ButtonAddNext").attr("disabled", true);
        //end
        <?php
        echo $this->ComboBoxStuProf->ajaxcall("SelectClass",array(),array('ComboBoxStuClass'));
    }

    function SelectClass($sender,$params)
    {
     global $dzsxDB;
     $this->ComboBoxStuClass->clear();
     $this->SelcetClassByProf($sender,$params);
     $_SESSION['TeacherExpinfo_Flag'] = 6;
    }

    function SelcetClassByProf($sender,$params)      //根据专业选择班级，并添加到ComboBoxStuClass中
    {
     global $dzsxDB;
     $this->ComboBoxStuClass->clear();
     $StuProfId=$params;
     $dzsxDB->QueryStuClass->Active = False;
     $dzsxDB->QueryStuClass->SQL ="Select ClassName From student Where ProfId='$StuProfId' Group By ClassName";
     $dzsxDB->QueryStuClass->Active = True;
     $dzsxDB->QueryStuClass->first();
     while( ! $dzsxDB->QueryStuClass->EOF)
      {
         $this->ComboBoxStuClass->AddItem($dzsxDB->QueryStuClass->ClassName);
         $dzsxDB->QueryStuClass->next();
      }

    }


   /*----------------------------------添加实训项目----------------------*/

    function ButtonAddJSClick($sender, $params)
    {
        ?>
        //begin js
         $.fancybox({
          'href':'#addsxxm'
		    });
        //end
        <?php
    }

   function ButtonQrAddJSClick($sender, $params)
    {
        ?>
        //begin js
        var params=new Array();
        params[0]=$("#ComboBoxCourseName").val();
        params[1]=$("#ComboBoxStuProf").val();
        params[2]=$("#ComboBoxStuClass").find("option:selected").text();
        //end
        <?php
       echo $this->ButtonQrAdd->ajaxCall("AddSxxm");
    }


   function AddSxxm($sender,$params) //向数据库表expinfo中添加记录
   {
      global $dzsxDB;
      $v = $_SESSION['TeacherId'];
      $add_CourseId = $params[0];//$this->ComboBoxCourseName->Items[$this->ComboBoxCourseName->ItemIndex];
      $add_StuProf = $params[1];
      $add_StuClass = $params[2];
      $add_shrq = date('Y-m-d');
      $add_ExpName = $this->EditExpName->text;
      $add_Keshi = $this->EditKeShi->text;
      //$dzsxDB->QueryTeacherExpinfo->Active=False;
      $dzsxDB->QueryAddExpinfo->Active = False;
      $dzsxDB->QueryAddExpinfo->SQL = "INSERT INTO expinfo
        (ExpName,TeacherId,CourseId,ProfId,ClassName,Expshrq,keshi,Expfbzt,Expshzt)
        VALUES('$add_ExpName','$v','$add_CourseId','$add_StuProf','$add_StuClass','$add_shrq','$add_Keshi',0,0)";
      $dzsxDB->QueryAddExpinfo->Active = True;

      //此处应添加验证添加的内容是否正确的代码
      /*----下面语句清除已经填写完的内容,解决刷新重复添加问题-------*/
      $dzsxDB->QueryAddExpinfo->Active = False;
      $this->ComboBoxCourseName->clear();
      $this->ComboBoxStuProf->clear();
      //$this->EditClassName->text = "";
      //$this->EditXuequ->text = "";
      $this->EditExpName->text = "";
      $this->EditKeShi->text = "";
      $dzsxDB->QueryAddExpinfo->SQL = "";
      if($params[3]!=1)
       $_SESSION['TeacherExpinfo_Flag'] =5;
      else
       $_SESSION['TeacherExpinfo_Flag']=0;
   }

    function ButtonAddNextJSClick($sender, $params)
    {
        ?>
        //begin js
        var params=new Array();
        params[0]=$("#ComboBoxCourseName").val();
        params[1]=$("#ComboBoxStuProf").val();
        params[2]=$("#ComboBoxStuClass").find("option:selected").text();
        params[3]= 1;
        //end
        <?php
        echo $this->ButtonAddNext->ajaxcall("AddSxxm");
    }


   function ButtonCanelAddClick($sender, $params)
   {
      $_SESSION['TeacherExpinfo_Flag'] = 0;
      redirect("Teacher.php");
   }

   /*--------------------删除被选中的实训项目-----------------*/
   function ButtonDeleteJSClick($sender, $params)
   {
      ?>
        //begin js
        //获取被选中的实训项目Id
        var tmp=$("#sxxmlist").getGridParam('selarrrow');  //获取所有input元素
        if(tmp[0]==null)
          alert("请选择要删除的实训项目！");
        else{
        var params=new Array();
        var i=0;
        if(confirm("确定要删除吗？"))
         {
          for(var i=0;i<tmp.length;i++)
           {
            params[i]=tmp[i];
           }
         }

         }
        //end
      <?php
      echo $this->ButtonDelete->ajaxcall("DelExpinfo");
   }


   function DelExpinfo($sender, $params)//在expinfo表中删除被选择的实训项目
   {
      global $dzsxDB;
      $_SESSION['TeacherExpinfo_Flag'] = 0;
      $i = 0;
      while($params[$i] != null)
      {
         $tmp = $params[$i];
         $dzsxDB->QueryDelExinfo->Active = False;
         $dzsxDB->QueryDelExinfo->SQL = "delete from expinfo where ExpId=$tmp And Expshzt=0";
         $dzsxDB->QueryDelExinfo->Active = True;
         $i++;
      }
      if($params[0] == null)
         $_SESSION['TeacherExpinfo_Flag'] = 4;
      else
         $_SESSION['TeacherExpinfo_Flag'] =1;
      //此处应添加检测是否成功删除的代码if(mysql_)
   }

   /*------根据$_SESSION['TeacherExpinfo_Flag']的不同值，ajax完成后，
   刷新页面，进行不同的操作提示，
   涉及到删除，发布操作--------*/
   function TeahcerAfterAjaxProcess($sender, $params)
   {
      global $ajaxResponse;
      switch($_SESSION['TeacherExpinfo_Flag'])
      {
         case 0:
            break;
         case 1: //$ajaxResponse->script('alert("删除完成！");location.replace("teacher.php");');
            //break;
         case 2: //$ajaxResponse->script('alert("发布完成！");location.replace("teacher.php");');
            //break;
         case 3: //$ajaxResponse->script('alert("取消发布！");window.location.href="teacher.php";');
            //break;
         case 4: //$ajaxResponse->script('alert("您未选择项目或取消了操作！");location.replace("teacher.php");');
                 // break;
         case 5:
         //$ajaxResponse->script('alert("添加成功！");window.location.href="teacher.php";');
               // break;
         case 7:$ajaxResponse->script("$('#sxxmshlist').trigger('reloadGrid');$('#sxxmlist').trigger('reloadGrid');");
                break;
         case 6:$ajaxResponse->script("$('#ButtonQrAdd').removeAttr('disabled');$('#ButtonAddNext').removeAttr('disabled');");
                break;

      }

   }

   /*----------------------------批量取消发布的实训项目------------------------*/
   function Button_qxfbJSClick($sender, $params)
   {
      ?>
        //begin js
         //获取被选中的实训项目Id
        var tmp=$("#sxxmlist").getGridParam('selarrrow');  //获取所有input元素
        var params=new Array();
        var i=0;
        if(confirm("确定要取消发布吗？"))
         {
          for(var i=0;i<tmp.length;i++)
           {
            params[i]=tmp[i];
           }
         }

        //end
      <?php
      echo $this->Button_qxfb->ajaxcall("qxfb");
   }

   function qxfb($sender, $params)
   {
      global $dzsxDB;
      $v = $_SESSION['TeacherId'];
      $_SESSION['TeacherExpinfo_Flag'] = 0;
      $i = 0;
      while($params[$i] != null)
       {
        $tmp=$params[$i];
        $dzsxDB->QueryFbExpinfo->Active = False;
        $dzsxDB->QueryFbExpinfo->SQL = "UPDATE expinfo SET Expfbzt=0 Where ExpId=$tmp
                                        And Expshzt=1";
        $dzsxDB->QueryFbExpinfo->Active = True;
        $i++;
       }
      if($params[0] == null)
         $_SESSION['TeacherExpinfo_Flag'] = 4;
      else
         $_SESSION['TeacherExpinfo_Flag'] = 3;
      //global $ajaxResponse;
   }

   /*----------------------------------发布被选中的实训项目---------------------*/
   function Button_fbJSClick($sender, $params)
   {
      ?>
        //begin js
        //获取被选中的实训项目Id
        var tmp=$("#sxxmlist").getGridParam('selarrrow');  //获取所有input元素
        var params=new Array();
        var i=0;
        if(confirm("确定要发布吗？"))
         {
          for(var i=0;i<tmp.length;i++)
           {
            params[i]=tmp[i];
           }
         }
         else
          {
           //$('#sxxmlist').trigger('reloadGrid');
           //window.location.href="teacher.php";
          }
        //end
      <?php
      echo $this->Button_fb->ajaxcall("FbExpinfo");
   }

   function FbExpinfo($sender, $params)//在数据库expinfo表中设置选择项目的fbzt
   {
      global $dzsxDB;
      $v = $_SESSION['TeacherId'];
      $_SESSION['TeacherExpinfo_Flag'] = 0;
      $i = 0;
      while($params[$i] != null)
       {
        $tmp=$params[$i];
        $dzsxDB->QueryFbExpinfo->Active = False;
        $dzsxDB->QueryFbExpinfo->SQL = "UPDATE expinfo SET Expfbzt=1 Where ExpId=$tmp
                                        And Expshzt=1";
        $dzsxDB->QueryFbExpinfo->Active = True;
        $i++;
       }
      if($params[0] == null)
         $_SESSION['TeacherExpinfo_Flag'] = 4;
      else
         $_SESSION['TeacherExpinfo_Flag'] = 2;
      //global $ajaxResponse;
   }


 /**
 实训项目的审核与取消审核
 **/

 //实训项目审核
  function ButtonsxxmshJSClick($sender, $params)
    {
      ?>
      //begin js
      var tmp=$("#sxxmshlist").getGridParam('selarrrow');  //获取所有input元素
      var params=new Array();
      var i=0;
      if(confirm("同意发布吗？"))
        {
          for(var i=0;i<tmp.length;i++)
           {
            params[i]=tmp[i];
           }
        }
      else
        $("#sxxmshlist").trigger("reloadGrid");
      //end
      <?php
      echo $this->Buttonsxxmsh->ajaxCall("sxxmsh");
    }

  function sxxmsh($sender,$params)
    {
     global $dzsxDB;
     $i = 0;
     while($params[$i]!=null)
       {
        $tmp=$params[$i];
        $Curdate=date('Y-m-d');
        $dzsxDB->Querysxxmsh->Active = False;
        $dzsxDB->Querysxxmsh->SQL = "UPDATE expinfo SET Expshzt=1,Expshrq='$Curdate' Where ExpId=$tmp";
        $dzsxDB->Querysxxmsh->Active = True;
        $i++;
       }
      if($params[0] == null)
         $_SESSION['TeacherExpinfo_Flag'] = 4;
      else
         $_SESSION['TeacherExpinfo_Flag'] = 7;
    }

  //实训项目取消审核
  function ButtonsxxmqxshJSClick($sender, $params)
    {
      ?>
      //begin js
      var tmp=$("#sxxmshlist").getGridParam('selarrrow');  //获取所有input元素
      var params=new Array();
      var i=0;
      if(confirm("禁止发布吗？"))
        {
          for(var i=0;i<tmp.length;i++)
           {
            params[i]=tmp[i];
            alert(params[i]);
           }
        }
      else
        $("#sxxmshlist").trigger("reloadGrid");
      //end
      <?php
      echo $this->Buttonsxxmqxsh->ajaxCall("sxxmqxsh");

    }


   function sxxmqxsh($sender,$params)
    {
     global $dzsxDB;
     $i = 0;
     while($params[$i]!=null)
       {
        $tmp=$params[$i];
        $dzsxDB->Querysxxmsh->Active = False;
        $dzsxDB->Querysxxmsh->SQL = "UPDATE expinfo SET Expshzt=0,Expfbzt=0 Where ExpId=$tmp";
        $dzsxDB->Querysxxmsh->Active = True;
        $i++;
       }
      if($params[0] == null)
         $_SESSION['TeacherExpinfo_Flag'] = 4;
      else
         $_SESSION['TeacherExpinfo_Flag'] = 7;
    }


   /*------------------------退出注销用户---------------------*/
   function ButtonLoginOutJSClick($sender, $params)
     {
        ?>
        //begin js
        window.close();
        //end
        <?php
        echo $this->ButtonLoginOut->ajaxCall("ButtonLoginOutClick");
     }
   function ButtonLoginOutClick($sender, $params)
    {
       $_SESSION = array();
       session_destroy();
    }
}



global $application;

global $Teahcer;

//Creates the form
$Teahcer = new Teahcer($application);

//Read from resource file
$Teahcer->loadResource(__FILE__);

//Shows the form
$Teahcer->show();

?>
